<?php

namespace app\admin\controller\booking;

use app\common\controller\Backend;

/**
 * 商品分类
 *
 * @icon fa fa-circle-o
 */
class GoodsCategory extends Backend
{

    /**
     * GoodsCategory模型对象
     * @var \app\admin\model\booking\GoodsCategory
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\booking\GoodsCategory;
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    private function resetChild($row, &$categoryList)
    {
        $arr = [];
        foreach ($categoryList as $key => $item) {
            if ($row['id'] == $item['pid']) {
                $arr[] = $item;
                unset($categoryList[$key]);
            }
        }
        return $arr;
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            $noKeyField = $this->request->param('noKeyField');
            $keyValue = $this->request->param('keyValue');

            if ($this->request->request('keyField') && $noKeyField != 100) {

                if (empty($keyValue)) {
                    $list = $this->selectpage()->getContent();
                    $list = (array)json_decode($list, true);
                    $list['list'] = array_merge([['id' => 0, 'name' => '无', 'pid' => 0]], $list['list']);
                    return json($list);
                }
                return $this->selectpage();
            }

            $categoryList = collection($this->model->order('weigh desc,id desc')->where(function ($query) use ($keyValue) {
                if (!empty($keyValue)) {
                    $query->where('id', 'IN', $keyValue);
                }
            })->select())->toArray();
            //只有两级，暂时不考虑多级;
            $data = [];
            //分离
            foreach ($categoryList as $key => $item) {
                if ($item['pid'] == 0) {
                    $childList = $this->resetChild($item, $categoryList);
                    $data[] = [
                        'parent' => $item,
                        'child'  => $childList
                    ];
                    unset($categoryList[$key]);
                }
            }
            //组合
            $newCategory = [];
            foreach ($data as $parent) {
                $newCategory[] = $parent['parent'];
                if (!empty($parent['child'])) {
                    $count = count($parent['child']);
                    foreach ($parent['child'] as $k => $child) {
                        if ($k == ($count - 1)) {
                            $child['name'] = '&nbsp;└&nbsp;' . $child['name'];
                        } else {
                            $child['name'] = '&nbsp;├&nbsp;' . $child['name'];
                        }
                        $newCategory[] = $child;
                    }
                }
            }
            //剩下的子级追加上
            foreach ($categoryList as $index => $value) {
                $newCategory[] = $value;
            }
            $search = $this->request->request("search");
            $type = $this->request->request("type");
            //构造父类select列表选项数据
            $list = [];
            foreach ($newCategory as $k => $v) {
                if ($search) {
                    if ($v['type'] == $type && stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
                        if ($type == "all" || $type == null) {
                            $list = $newCategory;
                        } else {
                            $list[] = $v;
                        }
                    }
                } else {
                    if ($type == "all" || $type == null) {
                        $list = $newCategory;
                    } elseif ($v['type'] == $type) {
                        $list[] = $v;
                    }
                }
            }
            $total = count($list);
            if ($noKeyField == 100) {
                return json([
                    "total" => $total,
                    "list"  => $list
                ]);
            }
            return json([
                "total" => $total,
                "rows"  => $list
            ]);
        }
        return $this->view->fetch();
    }
}
